package com.session2;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;

import javax.swing.JEditorPane;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.miginfocom.swing.MigLayout;

public class JSlideDemo extends JFrame implements ChangeListener, ActionListener {
	private JSlider sdVolumn;
	private JTextField txtvalue;
	private Timer timer;
	private JProgressBar proBar;
	private JFormattedTextField txtFormat;
	private JEditorPane txtEditor;
	public JSlideDemo(){
		this.timer = new Timer(1000,this);
		this.timer.start();
	}
	public void createFrom(){
		this.sdVolumn = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
//		this.sdVolumn.setMajorTickSpacing(10);
//		this.sdVolumn.setMinorTickSpacing(10);
		this.sdVolumn.setSnapToTicks(true);
		this.sdVolumn.setPaintTicks(true);
		this.sdVolumn.setPaintLabels(true);
		
		this.txtvalue = new JTextField(15);
		this.proBar = new JProgressBar(0,100);
		this.proBar.setOrientation(JProgressBar.HORIZONTAL);
		this.proBar.setStringPainted(true);
		
//		NumberFormat numberformat = NumberFormat.getInstance();
//		MaskFormatter myFormat = null;
//		try {
//			myFormat = new MaskFormatter("###,###,###");
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		this.txtFormat = new JFormattedTextField(myFormat);
		DateFormat dateFormat = DateFormat.getDateInstance();
		this.txtFormat = new JFormattedTextField(dateFormat);
		this.txtFormat.setColumns(15);
		this.setLayout(new MigLayout());
		
		this.getContentPane().add(this.sdVolumn,"wrap");
		this.getContentPane().add(this.txtvalue,"wrap");
		this.getContentPane().add(this.proBar,"wrap");
		this.getContentPane().add(this.txtFormat);
//		Dang Ki Su Kien
		this.sdVolumn.addChangeListener(this);
		this.proBar.addChangeListener(this);
	}
	public void showForm(){
		this.setTitle("Demo");
		this.setSize(400,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		JSlideDemo d = new JSlideDemo();
		d.createFrom();
		d.showForm();
	}
	@Override
	public void stateChanged(ChangeEvent e) {
		if(e.getSource() == this.sdVolumn){
			this.txtvalue.setText(this.sdVolumn.getValue()+"");
			this.proBar.setValue(this.sdVolumn.getValue());
			this.proBar.setString("Loading "+this.proBar.getValue());
		}
		if(e.getSource() == this.proBar){
			
		}
	}
	private boolean kt = true;
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == this.timer)
			if(this.sdVolumn.getValue() >= 100) kt = false;
			if(this.sdVolumn.getValue()<=0) kt = true;
			if(kt == true){
				this.sdVolumn.setValue(this.sdVolumn.getValue() + 5);
			
			}else 
				this.sdVolumn.setValue(this.sdVolumn.getValue() - 5);
	}
}
